package org.skyeye.modeling.customerized.ui;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.CreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteEntry;
import org.eclipse.gef.palette.PaletteGroup;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteSeparator;
import org.eclipse.gef.palette.PaletteStack;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.SelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.gef.tools.MarqueeSelectionTool;
import org.eclipse.jface.resource.ImageDescriptor;
import org.skyeye.modeling.customerized.model.ADInput;
import org.skyeye.modeling.customerized.model.AVOutput;
import org.skyeye.modeling.customerized.model.BulbInput;
import org.skyeye.modeling.customerized.model.DVOutput;
import org.skyeye.modeling.customerized.model.GPIOSoc;
import org.skyeye.modeling.customerized.model.GroundOutput;
import org.skyeye.modeling.customerized.model.SPDTSwitch;
import org.skyeye.modeling.customerized.model.SPSTSwitch;
import org.skyeye.modeling.customerized.model.SineWaveAVOutput;
import org.skyeye.modeling.customerized.model.SquareWaveAVOutput;

public class GPIOPaletteFactory {
	public static PaletteRoot createPalette() {
		PaletteRoot paletteRoot = new PaletteRoot();
		paletteRoot.addAll(createCategories(paletteRoot));
		return paletteRoot;
	}

	static private List createCategories(PaletteRoot root) {
		List categories = new ArrayList();

		categories.add(createControlGroup(root));
		categories.add(createComponentsDrawer());
		
		return categories;
	}


	static private PaletteContainer createComponentsDrawer() {

		PaletteDrawer drawer = new PaletteDrawer(LogicMessages.LogicPlugin_Category_Components_Label,
				ImageDescriptor.createFromFile(GPIOSoc.class, "icons/comp.gif"));

		List entries = new ArrayList();



		CombinedTemplateCreationEntry  combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_GPIOSOC_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_GPIOSOC_Description, new SimpleFactory(GPIOSoc.class),
				null, 
				null
		);
		entries.add(combined);
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_Bulb_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_Bulb_Description, new SimpleFactory(BulbInput.class),
				null, 
				null
		);
		entries.add(combined);

		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_AD_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_AD_Description, new SimpleFactory(ADInput.class),
				null, 
				null
		);
		entries.add(combined);
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_SquareWaveAV_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_SquareWaveAV_Description, new SimpleFactory(SquareWaveAVOutput.class),
				null, 
				null
		);
		entries.add(combined);
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_SineWaveAV_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_SineWaveAV_Description, new SimpleFactory(SineWaveAVOutput.class),
				null, 
				null
		);
		entries.add(combined);
		
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_DV_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_DV_Description, new SimpleFactory(DVOutput.class),
				null, 
				null
		);
		entries.add(combined);
		
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_Ground_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_Ground_Description, new SimpleFactory(GroundOutput.class),
				null, 
				null
		);
		entries.add(combined);
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_SPST_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_SPST_Description, new SimpleFactory(SPSTSwitch.class),
				null, 
				null
		);
		entries.add(combined);
		
		combined = new CombinedTemplateCreationEntry(LogicMessages.LogicPlugin_Tool_CreationTool_SPDT_Label,
				LogicMessages.LogicPlugin_Tool_CreationTool_SPDT_Description, new SimpleFactory(SPDTSwitch.class),
				null, 
				null
		);
		entries.add(combined);

		drawer.addAll(entries);
		return drawer;
	}

	static private PaletteContainer createControlGroup(PaletteRoot root) {
		PaletteGroup controlGroup = new PaletteGroup(LogicMessages.LogicPlugin_Category_ControlGroup_Label);

		List entries = new ArrayList();

		ToolEntry tool = new PanningSelectionToolEntry();
		entries.add(tool);
		root.setDefaultEntry(tool);

		PaletteStack marqueeStack = new PaletteStack(LogicMessages.Marquee_Stack, "", null); 
		marqueeStack.add(new MarqueeToolEntry());
		MarqueeToolEntry marquee = new MarqueeToolEntry();
		marquee.setToolProperty(MarqueeSelectionTool.PROPERTY_MARQUEE_BEHAVIOR,
				new Integer(MarqueeSelectionTool.BEHAVIOR_CONNECTIONS_TOUCHED));
		marqueeStack.add(marquee);
		marquee = new MarqueeToolEntry();
		marquee.setToolProperty(MarqueeSelectionTool.PROPERTY_MARQUEE_BEHAVIOR, new Integer(
				MarqueeSelectionTool.BEHAVIOR_CONNECTIONS_TOUCHED | MarqueeSelectionTool.BEHAVIOR_NODES_CONTAINED));
		marqueeStack.add(marquee);
		marqueeStack.setUserModificationPermission(PaletteEntry.PERMISSION_NO_MODIFICATION);
		entries.add(marqueeStack);

		tool = new ConnectionCreationToolEntry(
				LogicMessages.LogicPlugin_Tool_ConnectionCreationTool_ConnectionCreationTool_Label,
				LogicMessages.LogicPlugin_Tool_ConnectionCreationTool_ConnectionCreationTool_Description, null,
				ImageDescriptor.createFromFile(GPIOSoc.class, "icons/connection16.gif"), 
				ImageDescriptor.createFromFile(GPIOSoc.class, "icons/connection24.gif")
		);
		entries.add(tool);
		controlGroup.addAll(entries);
		return controlGroup;
	}

}
